import { MINUS, PLUS, RESET, TOGGLE_LOADING, UPDATE_COLOR } from './actionTypes'

const defaultState = {
    loading: false,
    count: 0,
    rgb: [255,255,255]
};  //默认数据
export default (state = defaultState, action) => {  //就是一个方法函数
    if (action.type === PLUS){
        let newState = JSON.parse(JSON.stringify(state)) //深度拷贝state
        newState.count = state.count +1
        return newState
    }
    if (action.type === MINUS) {
        let newState = JSON.parse(JSON.stringify(state)) //深度拷贝state
        newState.count = state.count - 1
        return newState
    }
    if (action.type === RESET) {
        let newState = JSON.parse(JSON.stringify(state)) //深度拷贝state
        newState.count = action.val
        return newState
    }
    if (action.type === TOGGLE_LOADING) {
        let newState = JSON.parse(JSON.stringify(state)) //深度拷贝state
        newState.loading = action.val
        return newState
    }
    if (action.type === UPDATE_COLOR) {
        let newState = JSON.parse(JSON.stringify(state)) //深度拷贝state
        newState.rgb[action.idx] = action.val
        return newState
    }
    return state
}